
package com.foreveross.fetl.web.action.preprocess;

import javax.inject.Inject;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.dayatang.querychannel.support.Page;
import com.foreveross.fetl.application.ITaskTimeApplication;
import com.foreveross.fetl.util.FetlErrorUtils;
import com.foreveross.fetl.vo.TaskTimeVO;
import com.foreveross.fetl.web.action.BaseAction;

@Controller("taskTimeAction")
@Scope("prototype")
public class TaskTimeAction extends BaseAction {
		
	private static final long serialVersionUID = 1L;
	
	private TaskTimeVO taskTimeVO = new TaskTimeVO();
		
	@Inject
	private ITaskTimeApplication taskTimeApplication;
	
	public String list() {
		return "METHOD";
	}
	
	public String view(){
		String id=getRequest().getParameter("id");
		Boolean judge=false;
		try {
			   Integer.parseInt(id);
			   judge = true;
			} catch (NumberFormatException e) {
			   judge = false;
			  }
		if(judge == true){
			long ids = Long.parseLong(id);
			taskTimeVO=taskTimeApplication.get(ids);
		}
		return "METHOD";
	}
	
	public String add(){
		return "METHOD";
	}
	
	public String update(){
		return "METHOD";
	}
	
	public String addFun() {
		taskTimeVO.setTaskCode(FetlErrorUtils.metadataForAirs);
		taskTimeVO.setTaskName(FetlErrorUtils.fetlNameMap.get(taskTimeVO.getTaskCode()));
		try {
			taskTimeApplication.save(taskTimeVO);
			dataMap.put("result", "success");
		} catch (Exception e) {
			e.printStackTrace();
			dataMap.put("result", e.getMessage());
		}
		return "JSON";
	}
	
	public String updateFun() {
		try {
			taskTimeApplication.save(taskTimeVO);
			dataMap.put("result", "success");
		} catch (Exception e) {
			e.printStackTrace();
			dataMap.put("result", e.getMessage());
		}
		
		return "JSON";
	}
	
	public String pageJson() {
		String channel=null;
		String code=null;
		if(null!=taskTimeVO){
			channel=taskTimeVO.getChannel();
			code=taskTimeVO.getTaskCode();
		}
		Page<TaskTimeVO> all = taskTimeApplication.pageQuery(channel,code, page, pagesize);
		dataMap.put("Rows", all.getResult());
		dataMap.put("start", page * pagesize - pagesize);
		dataMap.put("limit", pagesize);
		dataMap.put("Total", all.getTotalCount());
		return "JSON";
	}
	
	public String delete() {
	    String idsString = getRequest().getParameter("ids");
        if(idsString != null){
            String[] idArrs = idsString.split(",");
            Long[] ids = new Long[idArrs.length];
            for (int i = 0; i < idArrs.length; i ++) {
            	  ids[i] = Long.parseLong(idArrs[i]);
            	  TaskTimeVO vo=new TaskTimeVO();
            	  vo.setId(ids[i]);
            	  taskTimeApplication.delete(vo);              
           }
        }
        
		dataMap.put("result", "success");
		return "JSON";
	}
	
	public String get() {
		dataMap.put("data", taskTimeApplication.get(taskTimeVO.getId()));
		return "JSON";
	}

	public TaskTimeVO getTaskTimeVO() {
		return taskTimeVO;
	}

	public void setTaskTimeVO(TaskTimeVO taskTimeVO) {
		this.taskTimeVO = taskTimeVO;
	}

	

}